package org.dreamfly.core.action;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.util.Version;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.search.FullTextSession;
import org.hibernate.search.Search;

import java.util.List;

public class searchInfo {
    public static List search(Session session, String keywords) {
        FullTextSession fts = Search.getFullTextSession(session);
        Transaction tx = fts.beginTransaction();
        String searchQuery = "title:" + keywords;
        QueryParser parser = new QueryParser(Version.LUCENE_29, "title", new StandardAnalyzer(Version.LUCENE_29));
        org.apache.lucene.search.Query luceneQuery;
        try {
            luceneQuery = parser.parse(searchQuery);
        }
        catch (ParseException e) {
            throw new RuntimeException("Unable to parse query: " + searchQuery, e);
        }
        org.hibernate.Query query = fts.createFullTextQuery(luceneQuery);
        tx.commit();
        return query.list();
    }
}